package com.eascs.web.demo.action;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.UuidUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.Render;
import com.eascs.site.view.ViewContext;

@RestController
public class ActionRestController extends BaseRestController {
	private final Logger logger = LoggerFactory.getLogger(ActionRestController.class);

	@RequestMapping(value = "/demo/action/form/submit")
	public BaseRestResult<String> formSubmit(ActionForm actionForm) {
		return BaseRestResult.warn(actionForm.getName());
	}

	@RequestMapping(value = "/demo/action/form/ajax/obj")
	public BaseRestResult<ActionForm> ajaxObj() {
		ActionForm actionForm = new ActionForm();
		actionForm.setName("my name");
		BaseRestResult<ActionForm> resutl = BaseRestResult.buildRestResult(actionForm);
		return resutl;
	}

	@RequestMapping(value = "/demo/action/form/action-rest")
	public BaseRestResult<ActionForm> actionForm() {
		ActionForm actionForm = new ActionForm();
		actionForm.setName("my name1");
		return new ActionFormResutl(actionForm);
	}

	@RequestMapping(value = "/demo/action/ajax/load")
	public BaseRestResult<String> ajaxLoad() {
		return BaseRestResult.warn("ajaxLoad");
	}

	@RequestMapping(value = "/demo/action/info")
	public BaseRestResult<String> info() {
		return BaseRestResult.info("info:" + UuidUtil.generateShortUuid());
	}

	@RequestMapping(value = "/demo/action/info-reload")
	public BaseRestResult<String> infoReload() {
		BaseRestResult<String> result = BaseRestResult.info("we will reload").andRefresh();
		return result;
	}

	@RequestMapping(value = "/demo/action/warn")
	public BaseRestResult<String> warn() {
		return BaseRestResult.warn("warn:" + UuidUtil.generateShortUuid());
	}

	@RequestMapping(value = "/demo/action/eval")
	public BaseRestResult<String> eval() {
		return BaseRestResult.eval("alert(1);$.actionDialog('confirm-eval message')");
	}

	@RequestMapping(value = "/demo/action/confirm-redirect")
	public BaseRestResult<String> confirmRedirect() {
		BaseRestResult<String> result = BaseRestResult.confirm("we will redirect to http://www.baidu.com").andRedirect("http://www.eascs.com");
		return result;
	}

	@RequestMapping(value = "/demo/action/confirm-action-ajax-load")
	public BaseRestResult<String> confirmActionAjaxLoad() {
		BaseRestResult<String> result = BaseRestResult.confirm("we will ajax load /demo/action/warn").andActionAjaxLoad("/demo/action/warn");
		return result;
	}

	@RequestMapping(value = "/demo/action/confirm-eval")
	public BaseRestResult<String> confirmEval() {
		BaseRestResult<String> result = BaseRestResult.confirm("we will confirm-eval $.actionDialog('confirm-eval message')").andEval("$.actionDialog('confirm-eval message');");
		return result;
	}

	@RequestMapping(value = "/demo/action/warn-redirect")
	public BaseRestResult<String> warnRedirect() {
		return BaseRestResult.warn("we will redirect to http://www.eascs.com").andRedirect("http://www.eascs.com");
	}

	@RequestMapping(value = "/demo/action/warn-reload")
	public BaseRestResult<String> warnReload() {
		return BaseRestResult.warn("we will reload").andRefresh();
	}

	@RequestMapping(value = "/demo/action/error")
	public BaseRestResult<String> error() {
		return BaseRestResult.error("error:" + UuidUtil.generateShortUuid());
	}

	@RequestMapping(value = "/demo/action/redirect")
	public BaseRestResult<String> redirect() {
		return BaseRestResult.redirect("http://www.eascs.com");
	}

	@RequestMapping(value = "/demo/action/topRedirect")
	public BaseRestResult<String> topRedirect() {
		return BaseRestResult.topRedirect("http://www.eascs.com");
	}

	@RequestMapping(value = "/demo/action/refresh")
	public BaseRestResult<String> refresh() {
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/demo/action/topRefresh")
	public BaseRestResult<String> topRefresh() {
		return BaseRestResult.topRefresh();
	}

	@RequestMapping(value = "/demo/action/html")
	public HtmlRestResult html(ActionForm actionForm) {
		ViewContext.put("uuid", UuidUtil.generateShortUuid(4));
		ViewContext.put("data", actionForm.getName());
		return HtmlRestResult.html("sample/action/html");
	}

	@RequestMapping(value = "/demo/action/render-{name}")
	public BaseRestResult<Render[]> render(@PathVariable() String name) {
		Render idRender = new Render("#action_render_item", UuidUtil.generateShortUuid(4));
		Render classRender = new Render(".action-render-item", UuidUtil.generateShortUuid(4));
		Render[] renders = new Render[] { idRender, classRender };
		return BaseRestResult.render(renders);
	}

	@RequestMapping(value = "/demo/action/pop-html-fragment-url")
	public BaseRestResult<String> popHtmlFragmentUrl() {
		return BaseRestResult.popHtmlFragmentUrl("/demo/action/get-html-fragment?name=myname1&password=mypassword1");
	}

	@RequestMapping(value = "/demo/action/pop-html-fragment")
	public HtmlRestResult popHtmlFragment(ActionForm actionForm) {
		ViewContext.put("uuid", UuidUtil.generateShortUuid(4));
		ViewContext.put("data", actionForm.getName());
		return HtmlRestResult.popHtmlFragment("sample/action/html");
	}

}